package com.musicalevents.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.musicalevents.dao.GenericDAO;

@SuppressWarnings("unchecked")
public class GenericDAOImpl<T, PK extends Serializable> implements GenericDAO<T, PK> {
    
	private SessionFactory sessionFactory;
	private Class<T> type;
    
    public GenericDAOImpl(Class<T> type) {
        this.type = type;
    }
    
	public PK create(T o) {
        return (PK) getSession().save(o);
    }
    
	public T get(PK id) {
		return (T) getSession().get(type, id);
	}
	
	public List<T> getAll() {
        Criteria crit = getSession().createCriteria(type);
        return crit.list();
	}
	
    public void update(T o) {
        getSession().update(o);
    }
    
    public void delete(T o) {
        getSession().delete(o);
    }
    
    public Session getSession() {
    	return (sessionFactory.getCurrentSession());
    }
    
    public void setSessionFactory(SessionFactory sessionFactory) {
    	this.sessionFactory = sessionFactory;
    }
}